package com.yoki.loginview;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.yoki.loginview.Save.SaveString;
import com.yoki.loginview.db.AccountDBHelper;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener {

    private EditText UserTB;
    private EditText PwdTB;
    private Button LoginBtn;
    private CheckBox SaveCheck;
    private SharedPreferences preferences;
    private boolean savecheck;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UserTB = findViewById(R.id.CUserTB);
        PwdTB = findViewById(R.id.CPwdTB);
        LoginBtn = findViewById(R.id.CLoginBtn);
        SaveCheck = findViewById(R.id.SaveCheck);
        SaveCheck.setOnCheckedChangeListener(this);
        LoginBtn.setOnClickListener(this);
        preferences = getPreferences(Context.MODE_PRIVATE);
        savecheck = preferences.getBoolean("savecheck",false);
        editor = preferences.edit();
        UserTB.setHint("请输入用户名");
        PwdTB.setHint("请输入密码");
        if(savecheck)
        {
            SaveCheck.setChecked(true);
            UserTB.setText(preferences.getString("UserName",null));
            PwdTB.setText(preferences.getString("Password",null));
        }
        Intent intent=getIntent();
        String username = "";
        String password = "";
        if(intent !=null) {
            username = intent.getStringExtra("NameText");
            password = intent.getStringExtra("password");
            if(username!=null&&password!=null)
            {
                UserTB.setText(username);
                PwdTB.setText(password);
            }
        }
    }

    /*public void loginClick(View view)
    {
        String UserName = UserTB.getText().toString();
        String Password = PwdTB.getText().toString();
        Log.d("Main","用户名为："+UserName);
        Log.d("Main","密码为："+Password);
        if(UserName.equals(""))
        {
            UserTB.requestFocus();
            Toast.makeText(this,"用户名为空，请输入",Toast.LENGTH_SHORT).show();
        }
        else if(Password.equals(""))
        {
            PwdTB.requestFocus();
            Toast.makeText(this,"密码为空，请输入",Toast.LENGTH_SHORT).show();
        }
        else if(UserName.equals("admin") && Password.equals("123456"))
        {
            Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(MainActivity.this,UserActivity.class);
            startActivity(intent);
        }
        else
        {
            Toast.makeText(this,"用户名或密码有错误",Toast.LENGTH_SHORT).show();
        }
        //Toast.makeText(this,"点击了登录按钮",Toast.LENGTH_LONG).show();
    }*/
    public void GoRegClick(View view)
    {
        Intent intent=new Intent(MainActivity.this,RegisterActivity.class);
        startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        String UserName = UserTB.getText().toString();
        String Password = PwdTB.getText().toString();
        int GetTrue = 0,GetName = 0,GetNull=0;
        AccountDBHelper helper = new AccountDBHelper(MainActivity.this,AccountDBHelper.VERSION_1);
        Cursor cursor = helper.select(null,null);

        cursor.moveToFirst();
        for(int i=0;i<cursor.getCount();i++)
        {
            Log.d("Main","用户名为："+cursor.getString(1));
            Log.d("Main","密码为："+cursor.getString(2));
            if(UserName.equals(""))
            {
                UserTB.requestFocus();
                Toast.makeText(this,"用户名为空，请输入",Toast.LENGTH_SHORT).show();
                break;
            }
            else if(Password.equals(""))
            {
                PwdTB.requestFocus();
                Toast.makeText(this,"密码为空，请输入",Toast.LENGTH_SHORT).show();
                break;
            }
            else if(UserName.equals(cursor.getString(1)) && Password.equals(cursor.getString(2)))
            {
                SaveString.User=i;
                GetTrue++;
                break;
            }
            else if(UserName.equals(cursor.getString(1)))
            {
                GetName++;
            }
            else
            {
                GetNull++;
            }
            cursor.moveToNext();
        }
        if(GetTrue!=0)
        {
            Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(MainActivity.this,MainGridViewActivity.class);
            intent.putExtra("NameText",UserName);
            startActivity(intent);
            if(savecheck)
            {
                editor.putString("UserName",UserTB.getText().toString());
                editor.putString("Password",PwdTB.getText().toString());
                editor.commit();
            }
            finish();
        }
        else if(GetName!=0)
        {
            Toast.makeText(this,"用户名或密码有错误",Toast.LENGTH_SHORT).show();
        }
        else if(GetNull!=0||cursor.getCount()==0)
        {
            Toast.makeText(this,"账号未注册",Toast.LENGTH_SHORT).show();
        }
    }

    private SharedPreferences.Editor editor;

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if(compoundButton.getId()==R.id.SaveCheck)
        {
            editor.putBoolean("savecheck",b);
            editor.commit();
        }
    }
}
